// 1. 引入vue
import Vue from 'vue'
// 2. 引入vue的路由模块
import VueRouter from 'vue-router'
// 3. 引入某一个页面组件
import Home from '../views/Home/index.vue'

import Footer from '@/components/Footer.vue'

// import { isToken } from '@/request/index'

// 4. 将vue的路由模块注册到vue
Vue.use(VueRouter)

// 5. 定义路由对象
const routes = [
  {
    path: '/',
    redirect: '/home'
    // component: Home
  },
  {
    path: '/home',
    name: 'Home',
    // component: Home
    components: {
      default: Home,
      footer: Footer
    }
    // alias: '/'
  },
  {
    path: '/cart',
    name: 'Cart',
    components: {
      default: () => import(/* webpackChunkName: "cart" */ '../views/Cart/index.vue')
    }
    // beforeEnter (to, from, next) {
    //   if (localStorage.getItem('isLogin') === 'ok') {
    //     next()
    //   } else {
    //     next('/login')
    //   }
    // }
  },
  {
    path: '/kind',
    name: 'Kind',
    components: {
      default: () => import(/* webpackChunkName: "kind" */ '../views/Kind/index.vue'),
      footer: Footer
    }
  },
  {
    path: '/my',
    name: 'My',
    components: {
      default: () => import(/* webpackChunkName: "user" */ '../views/My/index.vue'),
      footer: Footer
    }
  },
  {
    path: '/detail/:qwe',
    name: 'Abc',
    components: {
      default: () => import(/* webpackChunkName: "pro" */ '../views/Detail/index.vue')
    },
    props: {
      default: true
    }
  },
  {
    path: '/login',
    name: 'Login',
    components: {
      default: () => import(/* webpackChunkName: "user" */ '../views/Login/index.vue')
    }
  },
  {
    path: '/register',
    name: 'Register',
    components: {
      default: () => import(/* webpackChunkName: "user" */ '../views/Register/index.vue')
    }
  },
  {
    path: '/order',
    name: 'Order',
    components: {
      default: () => import(/* webpackChunkName: "order" */ '../views/Order/index.vue')
    }
  },
  {
    path: '/orderDetail/:id',
    name: 'OrderDetail',
    components: {
      default: () => import(/* webpackChunkName: "order" */ '../views/OrderDetail/index.vue')
    },
    props: {
      default: true
    }
  }
]

// 6. 创建路由实例
const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  // 7. 将路由对象添加到创建路由实例的配置选项
  routes
})

// 全局路由守卫：所有的路由都会被拦截
// router.beforeEach((to, from, next) => {
// if (to.name === 'Abc' || to.name === 'Login' || to.name === 'Register' || to.name === 'Home') {
//   next()
// } else {
//   isToken().then(data => {
//     if (data.data.code === 400) {
//       next('/login')
//     } else {
//       next()
//     }
//   })
// }
// console.log(to, from)
//   if (localStorage.getItem('isLogin') === 'ok' || to.name === 'Login' || to.name === 'Register' || to.name === 'Home') {
//     next()
//   } else {
//     next('/login')
//   }
// })

// 7. 将路由实例暴露
export default router
